// pages/home/messageList/messageList.js
import * as API from '../../../api/contactsApi';
import * as util from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholder: '输入客户名称/客户编码搜索',
    searchKey: '',
    pageNo: 1,
    hasRequest: false,
    total: 0,
    networkError: false,
    dataSource: [],
    hasMore: true,
    customId: '',
    contactsId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      contactsId: options.contactsId,
      customId: options.customId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.clear();
    this.loadData(true);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 重新加载
    this.clear();
    this.loadData(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 增量加载
    if (this.data.hasMore) {
      this.setData({
        pageNo: ++this.data.pageNo
      });
      this.loadData(false);
    }
  },
  clear: function () {
    // 清空数据重新加载
    this.setData({
      dataSource: [],
      pageNo: 1,
      pageSize: 10,
      hasMore: true
    });
  },
  loadData: function (options) {
    var postbody = {
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
      id: this.data.contactsId,
      customId: this.data.customId
    }
    API.getContactsOrderList(postbody).then(res => {
      if (!this.data.hasMore) return;
      let arr = res.orderList || [];
      if (!options) {
        arr = this.data.dataSource.concat(res.orderList || []);
      }
      let hasMore = arr.length < this.data.dataSource.totalCount;
      this.setData({
        dataSource: arr,
        hasMore: hasMore,
        total: res.totalCount,
        hasRequest: true,
      });
    }, err => {

    });
  },
  goto: function (e) {
    util.preventMoreClick(this, () => {
      let url = e.currentTarget.dataset.to;
      const orderId = e.currentTarget.dataset.orderId;
      if (url && url.length) {
        if (orderId) {
          wx.navigateTo({
            url: url + '?id=' + orderId
          });
        }
      }
    })
  },

})